<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java String.intern() 方法</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            border-radius: 12px;
            padding: 1.5rem;
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .feature-card:hover {
            border-color: #667eea;
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.1);
        }
        
        .highlight-text {
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
            100% { transform: translateY(0px); }
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-code text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                深入理解 Java String.intern() 方法
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索字符串常量池的奥秘，掌握内存优化的关键技术
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card p-8 mb-8">
            <div class="flex items-start space-x-4">
                <div class="text-purple-600 text-3xl">
                    <i class="fas fa-lightbulb"></i>
                </div>
                <div class="flex-1">
                    <h2 class="text-2xl font-bold mb-4 text-gray-800">核心概念</h2>
                    <p class="text-lg text-gray-700 leading-relaxed">
                        <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">String.intern()</code> 
                        方法是 Java 中一个强大的内存优化工具。它通过将字符串放入字符串常量池（String Constant Pool），
                        或返回常量池中已有字符串的引用，帮助我们减少内存占用并提升性能。
                    </p>
                </div>
            </div>
        </div>

        <!-- Working Principle -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card p-8">
                <h2 class="section-title text-3xl font-bold text-gray-800">
                    <i class="fas fa-cogs text-purple-600 mr-3"></i>工作原理
                </h2>
                <div class="space-y-6">
                    <div class="feature-card">
                        <div class="flex items-center mb-3">
                            <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center font-bold mr-3">1</span>
                            <h3 class="font-semibold text-lg">检查常量池</h3>
                        </div>
                        <p class="text-gray-700">JVM 首先检查字符串常量池中是否存在相同内容的字符串</p>
                    </div>
                    <div class="feature-card">
                        <div class="flex items-center mb-3">
                            <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center font-bold mr-3">2</span>
                            <h3 class="font-semibold text-lg">返回引用</h3>
                        </div>
                        <p class="text-gray-700">如果存在，直接返回常量池中该字符串的引用</p>
                    </div>
                    <div class="feature-card">
                        <div class="flex items-center mb-3">
                            <span class="bg-purple-600 text-white rounded-full w-8 h-8 flex items-center justify-center font-bold mr-3">3</span>
                            <h3 class="font-semibold text-lg">添加到常量池</h3>
                        </div>
                        <p class="text-gray-700">如果不存在，将字符串添加到常量池并返回引用</p>
                    </div>
                </div>
            </div>

            <div class="content-card p-8">
                <h2 class="section-title text-3xl font-bold text-gray-800">
                    <i class="fas fa-rocket text-purple-600 mr-3"></i>使用场景
                </h2>
                <div class="space-y-6">
                    <div class="bg-gradient-to-r from-purple-50 to-blue-50 p-6 rounded-lg">
                        <h3 class="font-semibold text-xl mb-3 text-purple-800">
                            <i class="fas fa-memory mr-2"></i>避免重复字符串
                        </h3>
                        <p class="text-gray-700">
                            当应用中存在大量内容相同的字符串时，使用 <code class="bg-white px-2 py-1 rounded text-purple-600">intern()</code> 
                            确保它们共享相同的内存引用，显著减少内存占用。
                        </p>
                    </div>
                    <div class="bg-gradient-to-r from-blue-50 to-purple-50 p-6 rounded-lg">
                        <h3 class="font-semibold text-xl mb-3 text-blue-800">
                            <i class="fas fa-tachometer-alt mr-2"></i>优化性能
                        </h3>
                        <p class="text-gray-700">
                            在频繁的字符串比较操作中，使用 <code class="bg-white px-2 py-1 rounded text-blue-600">==</code> 
                            比较引用而非内容，大幅提升比较效率。
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Visualization -->
        <div class="content-card p-8 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-project-diagram text-purple-600 mr-3"></i>内存结构可视化
            </h2>
            <div class="mermaid">
                graph TD
                    A[堆内存 Heap] --> B[String对象 s1]
                    A --> C[字符串常量池]
                    C --> D["hello" 引用]
                    E[栈内存 Stack] --> F[s1 变量]
                    E --> G[s2 变量]
                    E --> H[s3 变量]
                    F -.-> B
                    G -.-> D
                    H -.-> D
                    B -->|intern()| D
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style C fill:#bbf,stroke:#333,stroke-width:2px
                    style E fill:#bfb,stroke:#333,stroke-width:2px
                    style D fill:#fbb,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Code Example -->
        <div class="content-card p-8 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-code text-purple-600 mr-3"></i>示例代码
            </h2>
            <div class="code-block p-6 overflow-x-auto">
                <pre class="text-sm md:text-base"><code><span class="text-blue-400">public class</span> <span class="text-yellow-300">StringInternDemo</span> {
    <span class="text-blue-400">public static void</span> <span class="text-yellow-300">main</span>(<span class="text-blue-400">String</span>[] args) {
        <span class="text-gray-500">// 在堆中创建新的字符串对象</span>
        <span class="text-blue-400">String</span> s1 = <span class="text-blue-400">new</span> <span class="text-blue-400">String</span>(<span class="text-green-300">"hello"</span>);
        
        <span class="text-gray-500">// 获取常量池中的引用</span>
        <span class="text-blue-400">String</span> s2 = s1.<span class="text-yellow-300">intern</span>();
        
        <span class="text-gray-500">// 直接引用常量池中的字符串</span>
        <span class="text-blue-400">String</span> s3 = <span class="text-green-300">"hello"</span>;

        System.out.<span class="text-yellow-300">println</span>(s1 == s2); <span class="text-gray-500">// false - s1在堆中，s2在常量池中</span>
        System.out.<span class="text-yellow-300">println</span>(s2 == s3); <span class="text-gray-500">// true - 都指向常量池中的同一个对象</span>
    }
}</code></pre>
            </div>
            <div class="mt-6 grid md:grid-cols-3 gap-4">
                <div class="bg-red-50 p-4 rounded-lg border-l-4 border-red-400">
                    <p class="font-semibold text-red-800 mb-1">s1 对象</p>
                    <p class="text-sm text-gray-700">堆内存中的独立对象</p>
                </div>
                <div class="bg-green-50 p-4 rounded-lg border-l-4 border-green-400">
                    <p class="font-semibold text-green-800 